package org.bugeden.netdisk.user

import java.util.Properties
import java.io.{FileNotFoundException, File, FileInputStream}

/**
 * Created by IntelliJ IDEA.
 * User: inotseeyou
 * Date: 2010-11-13
 * Time: 18:03:53
 * To change this template use File | Settings | File Templates.
 */

class User(val name: String, val psw: String) extends AnyRef with IUser{

  val config = new Properties
  config.put("user.name", name)
  config.put("user.password", psw)

  def setConfig(props: Properties): Unit = {
    config.putAll(props)
  }

  def sendProtocol() = config.getProperty("user.send.protocol")

  def sendHostPort() = Integer.parseInt(config.getProperty("user.send.hostport"))

  def sendHostName() = config.getProperty("user.send.hostname")

  def revProtocol() = config.getProperty("user.rev.protocol")

  def revHostPort() = Integer.parseInt(config.getProperty("user.rev.hostport"))

  def revHostName() = config.getProperty("user.rev.hostname")

  def userType() = config.getProperty("user.type")

  def password() = config.getProperty("user.password")

  def userName() = config.getProperty("user.name")
}

object User{
  def apply(name: String, psw: String, cfgFile: String): User = {
    val file = new File(cfgFile)
    if(!file.isFile) throw new FileNotFoundException("can't not find [%s]".format(cfgFile))
    val props = new Properties
    props.load(new FileInputStream(file))
    val user = new User(name, psw)
    user.setConfig(props)
    user
  }

  def apply(): User = {
    val is = ClassLoader.getSystemClassLoader.getResourceAsStream("META-INF/mailbox-config.properties")
    val user = new User("", "")
    val cfg = new Properties
    cfg.load(is)
    user.setConfig(cfg)
    user
  }
}